package com.csh.auth.domain.service.impl;

import com.csh.auth.common.enums.IsDeletedFlagEnum;
import com.csh.auth.domain.entity.AuthRolePermissionBo;
import com.csh.auth.domain.service.AuthRolePermissionDomainService;
import com.csh.auth.infra.basic.entity.AuthRolePermission;
import com.csh.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {
    @Resource
    private AuthRolePermissionService authRolePermissionService;

//    @Override
//    public Boolean add(AuthRolePermissionBo authRolePermissionBo) {
//        List<AuthRolePermission> authRolePermissionList = new LinkedList<>();
//        Long roleId = authRolePermissionBo.getRoleId();
//        authRolePermissionBo.getPermissionIdList().forEach(permissionId -> {
//            AuthRolePermission authRolePermission = new AuthRolePermission();
//            authRolePermission.setRoleId(roleId);
//            authRolePermission.setPermissionId(permissionId);
//            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//            authRolePermissionList.add(authRolePermission);
//        });
//        int count = authRolePermissionService.batchInsert(authRolePermissionList);
//        return count > 0 ;
//    }

    @Override
    public Boolean add(AuthRolePermissionBo authRolePermissionBo) {
        Long roleId = authRolePermissionBo.getRoleId();
        List<AuthRolePermission> authRolePermissionList = authRolePermissionBo.getPermissionIdList().stream().map(
                permissionId -> {
                    AuthRolePermission authRolePermission = new AuthRolePermission();
                    authRolePermission.setRoleId(roleId);
                    authRolePermission.setPermissionId(permissionId);
                    authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                    return authRolePermission;
                }).collect(Collectors.toList());
        int count = authRolePermissionService.batchInsert(authRolePermissionList);
        return count > 0;
    }

}
